define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/vips/models'),
        utils = require('js/utils/tools'),
        moment = require('moment'),
        app = Backbone.history;
    require('datetimepicker');

    var BackVipsListView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch',
            'click #export': 'exportData',
        },

        initialize: function (options) {
            this.options = options;
            this.collection = new models.BackVipsCollection();
            this.collection.bind('change remove reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        exportData: function () {
            notify.warning('警告', '导出中，请等待');
            utils.exportData(this.collection.url, this.options);
        },
        doSearch: function () {
            var options = {},
                searched = {};
            searched.calc_at_lower = $('#searchCalcAtLower').val();
            searched.calc_at_upper = $('#searchCalcAtUpper').val();
            searched.call_at_lower = $('#searchCallAtLower').val();
            searched.call_at_upper = $('#searchCallAtUpper').val();
            if (searched.calc_at_lower || searched.calc_at_upper) {
                options.calc_at = {};
                if (searched.calc_at_lower) options.calc_at.$gte = searched.calc_at_lower;
                if (searched.calc_at_upper) options.calc_at.$lt = searched.calc_at_upper;
            }
            if (searched.call_at_lower || searched.call_at_upper) {
                options.call_at = {};
                if (searched.call_at_lower) options.call_at.$gte = searched.call_at_lower;
                if (searched.call_at_upper) options.call_at.$lt = searched.call_at_upper;
            }
            utils.saveSearched('vips/back', searched);
            app.navigate(utils.composeQueryString('#vips/back/', options), {
                trigger: true
            });
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                searched: utils.getSearched('vips/back')
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || 'id',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#vips/back/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            $('.date-box').datetimepicker({
                format: 'YYYY-MM-DD'
            });
            utils.getPaginator(that.options, this.collection.total, '#vips/back/');
            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "vips/BackVips.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            if (!this.options.calc_at) this.options.calc_at = moment().format('YYYY-MM-DD');
            var view = new BackVipsListView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });

});